<template>
  <div>
    <sticky-top>
      <div class="title">
        <span>{{ isCreate ? '创建部门' : '更新部门' }}</span>
        <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
        <el-divider></el-divider>
      </div>
    </sticky-top>
    <div class="container">
      <div class="wrap">
        <el-row>
          <el-col :lg="16" :md="20" :sm="24" :xs="24">
            <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
              <el-form-item label="部门编号" prop="id_self_defined">
                <el-input size="medium" v-model="form.id_self_defined" placeholder="请填写部门编号"></el-input>
              </el-form-item>
<!--              <el-form-item label="上级编号" prop="higher_level_unit">-->
<!--                <el-input size="medium" v-model="form.higher_level_unit" placeholder="请填写上级部门编号"></el-input>-->
<!--              </el-form-item>-->
<!--              <el-form-item>-->
<!--                <el-autocomplete-->
<!--                  @focus="loadHigherUnitIdSuggestions"-->
<!--                  class="inline-input"-->
<!--                  v-model="this.higherUnitIdList"-->
<!--                  :fetch-suggestions="querySearch"-->
<!--                  placeholder="请输入内容"-->
<!--                  @select="handleHigherUnitIdSelect"-->
<!--                ></el-autocomplete>-->
<!--              </el-form-item>-->
              <el-form-item label="一级部门">
<!--                <el-radio v-model="is_higherUnit" label = 1 border @change="changeShow()"> 是 </el-radio>-->
<!--                <el-radio v-model="is_higherUnit" label = 0 border @change="changeShow()"> 否 </el-radio>-->
                <el-radio-group v-model="is_higherUnit">
                  <el-radio :label= true border>是</el-radio>
                  <el-radio :label= false border>否</el-radio>
<!--                  <el-divider>一级部门不用填上级编号(默认为0)</el-divider>-->
                      
                </el-radio-group>

              </el-form-item>

              <el-form-item label="上级编号" prop="" v-if="!is_higherUnit">
                <el-autocomplete
                  @focus="loadHigherIdSuggestions"
                  class="inline-input"
                  v-model="form.higher_level_unit"
                  :fetch-suggestions="queryCategorySearch"
                  placeholder="请输入内容"
                  @select="handleSelect">
                  <template slot-scope="{ item }">
                    <span class="id">{{ item.id_self_defined}}</span>
                    <span class="name">{{ item.name }}</span>
                  </template>
                </el-autocomplete>
              </el-form-item>
              <el-form-item label="部门名称" prop="name">
                <el-input size="medium" v-model="form.name" placeholder="请填写部门名称"></el-input>
              </el-form-item>
              <el-form-item label="部门电话" prop="phone">
                <el-input size="medium" v-model="form.phone" placeholder="请填写部门电话"></el-input>
              </el-form-item>
              <el-form-item label="部门备注" prop="comment">
                <el-input size="medium" v-model="form.comment" placeholder="请填写部门备注"></el-input>
              </el-form-item>
              <el-form-item label="是否有效" prop="is_use">
                <el-switch
                  style="display: block"
                  active-color="#13ce66"
                  v-model="form.is_use"
                  inactive-color="#ff4949"
                  active-text="有效"
                  inactive-text="无效"
                >
                </el-switch>
<!--                <el-input size="medium" v-model="form.is_use" placeholder="请填写部门是否有效"></el-input>-->
              </el-form-item>

              <el-form-item class="submit">
                <el-button
                  v-permission="{ permission: ['创建部门', '更新部门'], type: 'disabled' }"
                  type="primary"
                  @click="submitForm('form')"
                >保 存</el-button
                >
                <el-button @click="resetForm('form')">重 置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import Unit from '@/model/ag_unit'
export default {
  props: {
    isCreate: {
      type: Boolean,
      default: false,
    },
    unitId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      form: {
        id_self_defined:'',
        higher_level_unit:'0',
        name:'',
        phone: '',
        comment: '',
        is_use: '',
      },
      higher_id:[],
      is_higherUnit:true,
      rules: {
        minWidth: 100,
        minHeight: 100,
        maxSize: 5,
      },
    }
  },
  async mounted() {
    if (!this.isCreate) {
      // const res =  Unit.getUnit(this.unitId)
      const res = await Unit.getUnit(this.unitId)
      // this.getHigherUnitId()
      this.form = res
      if (this.form.is_use===1){
        this.form.is_use=true;
      }
    }else {
        this.form.is_use=false;
    }
  },
  methods: {

    async loadHigherIdSuggestions() {
      if (this.higher_id.length > 0) {
        return
      }
      try {
        this.higher_id = await Unit.getUnitList()
        // console.log(this.higher_id)
        if (this.higher_id.length === 0) {
          this.$message.error('暂无楼栋信息，请先添加')
        }
      } catch (error) {
        this.$message.error('获取楼栋信息失败')
        console.error(error)
      }
    },

    queryCategorySearch(queryString, cb) {
      // eslint-disable-next-line
      const suggestions = this.higher_id
      const results = queryString ? suggestions.filter(this.createCategoryFilter(queryString)) : suggestions
      cb(results)
    },
    createCategoryFilter(queryString) {
      // eslint-disable-next-line
      return suggestion => {
        return suggestion.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0
      }
    },
    handleSelect(item) {
      // this.buildingName = item.name
      // this.form.floor =item.name+'-'+this.form.floor
      // if (item.higher_level_unit == 0){
      //   this.form.higher_level_unit = '0'
      // }
      // else{
      //   this.form.higher_level_unit = item.id_self_defined
      // }
      // this.form.higher_unit_id = item.id_self_defined
      this.form.higher_level_unit = item.id_self_defined
    },

    // eslint-disable-next-line
    async submitForm(formName) {
      // await this.getValue()
      if (this.form.is_use===true){
        this.form.is_use=1;
      }else {
        this.form.is_use=0;
      }
      const postData = { ...this.form }
      // postData.spec_key_id_list = this.specs.map(spec => spec[0])
      let res
      if (this.isCreate) {
        res = await Unit.addUnit(postData)
      } else {
        res = await Unit.editUnit(this.unitId, postData)
      }
      this.$message.success(`${res.message}`)
      this.$emit('editClose')
      // if (res.code < window.MAX_SUCCESS_CODE) {
      //   this.$message.success(`${res.message}`)
      //   // this.resetForm(formName)
      //   this.$confirm('是否返回上一页', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning',
      //   }).then(async () => {
      //     this.$emit('editClose')
      //   })
      // }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('editClose')
    },
    changeShow(row){
      // this.is_higherUnit=!this.is_higherUnit
      console.log(row)
    }

  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}

.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;
    display: inline-flex;
    align-content: space-around;
    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .id {
      margin-right: 15px;
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
</style>
